package panel.viewSpots.navigationNew
{
	import assists.SizeManager;
	import assists.media.SoundMedia;
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.GeolocationEvent;
	import flash.events.MouseEvent;
	import flash.sensors.Geolocation;
	
	import panel.viewSpots.navigation.ArrowItem;
	import panel.viewSpots.navigation.NavigationBtnGroup;
	
	public class NavigationPathPanel extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _data:Object;
		
		private var _mapLayer:Sprite = new Sprite();
		private var _btnLayer:Sprite = new Sprite();
		
		public static const TYPE_NORMAL:int = 1;
		public static const TYPE_SCALE:int = 2;
		private var _map:Bitmap;
		private var _scaleNum:Number;
		private var _arrowList:Array = [];

		private var _oldHei:Number;
		private var _oldWid:Number;
		private var _btnsGroup:NavigationBtnGroup;
		private var _paopaoComp:PaoPaoSimPanel;
		private var _musicFunc:Function;
		private var _soundMedia:SoundMedia;
		private var _openDetailFunc:Function;
		private var _searchPanel:NavigationSearchPanel;
		private var _geoloc:Geolocation;
		private var _currentPlaceID:int = 1;//用户当前所在的景点的ID
		private var _historyList:Array = [];
		private var _currentPath:Object;
		private var _openWeibo:Function;
		private var _wcList:Array = [];
		
		
		public function NavigationPathPanel(stage:Stage,loader:ResourceLoader,data:Object,map:Bitmap)
		{
			_stage = stage;
			_loader = loader;
			_data = data;
			_map = map;
			trace(stage.height);
			init();
		}
		
		public function onFunc(...args):void
		{
			_musicFunc = args[0];
			_openDetailFunc = args[1];
			_openWeibo = args[2];
		}
		
		public function set onSoundMedia(value:SoundMedia):void
		{
			_soundMedia = value;
			_btnsGroup.onSoundMedia = value;
		}
		
		private function init():void
		{
			_mapLayer.addChild(_map);
			this.addChild(_mapLayer);
			this.addChild(_btnLayer);
		}
		
		public function show(pathType:int,type:int = 1):void
		{
			changeScale();
			addPath(pathType);
			
		}
		
		public function changePath(type:int):void
		{
			var path:Array = usePath(type);
			var spotInfo:Object;
			for(var i:int=0;i<path.length;i++)
			{
				var arrow:ArrowItem = _arrowList[i];
				spotInfo = _data.viewspots[path[i] - 1];
				if(!arrow)
				{
					arrow = new ArrowItem(_loader);
				    arrow.data = spotInfo;
					_arrowList.push(arrow);
					_mapLayer.addChild(arrow);
				}
				
				arrow.label = i + "";
				arrow.changeLabelVisible(false);
				arrow.x = spotInfo.posX * _scaleNum;
				arrow.y = spotInfo.posY * _scaleNum;
				
			}
			if(path.length < _arrowList.length)
			{
				var item:ArrowItem;
				for(i=path.length;i < _arrowList.length;i++)
				{
					item = _arrowList[i];
					_mapLayer.removeChild(item);
					item.removeEventListener(MouseEvent.CLICK,clickArrowHandler);
					_arrowList.splice(i,1);
					-- i;
				}
			}
		}
		
		private function usePath(type:int):Array
		{
			var path:Array = [];
			if(type == NavigationTypeChoose.TYPE_CAR)
			{
				_currentPath = _data.carPath;
				path = _data.carPath.path;
			}else if(type == NavigationTypeChoose.TYPE_NORMAL)
			{
				_currentPath = _data.normalPath;
				path = _data.normalPath.path;
			}else if(type == NavigationTypeChoose.TYPE_SPEED)
			{
				_currentPath = _data.speedPath;
				path = _data.speedPath.path;
			}
			return path;
		}
		
		private function addPath(type:int):void
		{
			if(type == 0) return;
			var path:Array = usePath(type);
			var spotInfo:Object;
			for(var i:int=0;i<path.length;i++)
			{
				var arrow:ArrowItem = new ArrowItem(_loader);
				arrow.currentPlaceID = _currentPlaceID;
				spotInfo = _data.viewspots[path[i] - 1];
				arrow.data = spotInfo;
				arrow.label = (i + 1) + "";
				arrow.x = spotInfo.posX * _scaleNum;
				arrow.y = spotInfo.posY * _scaleNum;
				arrow.changeLabelVisible(false);
				arrow.addEventListener(MouseEvent.CLICK,clickArrowHandler);
				_mapLayer.addChildAt(arrow,1);
				_arrowList.push(arrow);
			}
		}
		
		protected function clickArrowHandler(event:MouseEvent):void
		{
			if(!_btnsGroup) return;
			var arrow:ArrowItem = event.currentTarget as ArrowItem;
			center(arrow.x,arrow.y);
			if(!_paopaoComp)
			{
				_paopaoComp = new PaoPaoSimPanel(_loader,_stage);
				_paopaoComp.onMusicFunc = _musicFunc;
				_paopaoComp.onDetailFunc = _openDetailFunc;
				_mapLayer.addChild(_paopaoComp);
			}
			_paopaoComp.info = arrow.data;
			_paopaoComp.changeLabel(arrow.data.name,arrow.data.id);
				_paopaoComp.x = arrow.x;
				_paopaoComp.y = arrow.y - arrow.height * 1.5;
			_paopaoComp.visible = true;
				
		}
		
		private function changeScale():void
		{
//			计算最小的一边
			_oldHei = _map.height;
			_oldWid = _map.width;
			if(_map.width <= _map.height)
			{
				_map.height = _stage.stageHeight;
				_scaleNum = _map.height / _oldHei;
				_map.width = _scaleNum * _oldWid;
			    _mapLayer.x = (_stage.stageWidth - _mapLayer.width) * 0.5;
			}else
			{
				_map.width = _stage.stageWidth;
				_scaleNum = _map.width / _oldWid;
				_map.height = _scaleNum * _oldHei;
			    _mapLayer.y = (_stage.stageHeight - _mapLayer.height) * 0.5;
			}
		}
		
		public function showNormal():void
		{
			_map.width = _oldWid;
			_map.height = _oldHei;
			if(_data.viewspots[0])
			{
			var offX:Number = - _data.viewspots[0].posX + _stage.stageWidth * 0.5;
			var offY:Number = - _data.viewspots[0].posY + _stage.stageHeight * 0.5;
			if(offX > 0) offX = 0;
			if(offY > 0) offY = 0;
			_mapLayer.x = offX;
			_mapLayer.y = offY;
			
			var arrow:ArrowItem;
			for(var i:int=0;i<_arrowList.length;i++)
			{
				arrow = _arrowList[i];
				arrow.changeLabelVisible(true);
				arrow.x = arrow.x / _scaleNum;
				arrow.y = arrow.y / _scaleNum;
			}
			}else
			{
				_mapLayer.x = 0;
				_mapLayer.y = 0;
			}
			_mapLayer.addEventListener(MouseEvent.MOUSE_DOWN,moveMapHandler);
			if(!_btnsGroup)
			{
				_btnsGroup = new NavigationBtnGroup(_loader,_stage,null);
				_btnsGroup.init(changeMapScale,searchFunc,navigationFunc,_openWeibo,changeArrowsVisible,showWc);
				_btnLayer.addChild(_btnsGroup);
			}
		}
		
		private function navigationFunc():void
		{
//	seekMyPos(36.899897222222,122.39898888889);//大明神上的点
			seekMyPos(36.902258333333,122.39157777778);//赤山禅院与极乐菩萨界之间
			return;
			if(Geolocation.isSupported)
			{
				if(!_geoloc) _geoloc = new Geolocation();
				if(_geoloc.muted == false)
				{
					_geoloc.setRequestedUpdateInterval(3000);
				  _geoloc.addEventListener(GeolocationEvent.UPDATE,onGeolocUpdate);	
				}
			}
		}
		
		protected function onGeolocUpdate(e:GeolocationEvent):void
		{
			var str:String = "高度：" + e.altitude + "米\n";
				str = str + "经度：" +　e.longitude + "\n";
				str = str + "纬度：" +　e.latitude + "\n";
				str = str + "水平经度：" +　e.horizontalAccuracy + "\n";
				str = str + "垂直经度：" +　e.verticalAccuracy + "\n";
				str = str + "移动速度：" +　e.speed + "\n";
			trace(str);
			if(e.horizontalAccuracy > 100) return;
			_geoloc.removeEventListener(GeolocationEvent.UPDATE,onGeolocUpdate);	
			seekMyPos(e.latitude,e.longitude);
		}
		
		private function seekMyPos(longitude:Number,latitude:Number):void
		{
			var posInfo:Object = {};
			var info:Object;
			var dis:Number = 99;
			var longitudeDis:Number;
			var latitudeDis:Number;
			var minDis:Number;
			var posID:String;
			var disList:Array = [];
			for(var i:int=0;i<_data.viewspots.length;i++)
			{
				info = _data.viewspots[i];
				longitudeDis = info.gpsX - longitude;
				latitudeDis = info.gpsY - latitude;
				if(Math.abs(longitudeDis) < info.offset && Math.abs(latitudeDis) < info.offset)
				{
					posID = info.name;
					_historyList.push(info);
					changeArrowsColor(info);
					updateTopInfo();
					return;
				}else
				{
					minDis = Math.sqrt(Math.abs(longitudeDis * longitudeDis - latitudeDis * latitudeDis));
					if(dis > minDis)
					{
						disList.push(info);
						dis = minDis;
					}
				}
			}
	updateTopInfo("您在"+disList[disList.length - 1].name + "----" + disList[disList.length - 2].name);
		}
		
		private function updateTopInfo(str:String = null):void
		{
			if(str != null)
			{
				_btnsGroup.update(str,0);
			}
			if(_historyList.length == 0)
			{
				return;
			}else
			{
			for(var i:int=0;i<_currentPath.path.length;i++)
			{
				var spendTime:Number;
				if(_currentPath.path[i] == _historyList[_historyList.length - 1].id)
				{
					spendTime = _currentPath.time[i];
					break;
				}
			}
			_btnsGroup.update(_historyList[_historyList.length - 1].name,spendTime);
			}
		}
		
		private function changeArrowsColor(posSpotInfo:Object):void
		{
			var arrow:ArrowItem;
			for(var i:int=0;i<_arrowList.length;i++)
			{
				arrow = _arrowList[i];
				arrow.currentPlaceID = posSpotInfo.id;
				arrow.isVisited(true);
				if(arrow.data.id == posSpotInfo.id)
				{
					break;
				}
			}
		}
		
		private function searchFunc():void
		{
			if(!_searchPanel)
			{
				_searchPanel = new NavigationSearchPanel(_stage,_loader);
				_searchPanel.onOpenDetail(_openDetailFunc);
			}
				_searchPanel.show(_data.viewspots,_data.viewspots[0]);
				this.addChild(_searchPanel);
		}
		
		protected function moveMapHandler(event:MouseEvent):void
		{
			if(event.currentTarget as ArrowItem != null )	return;
			_mapLayer.startDrag();
			_stage.addEventListener(MouseEvent.MOUSE_UP,touchEndHandler);
			_stage.addEventListener(Event.ENTER_FRAME,frameHandler);
		}
		
		protected function touchEndHandler(event:MouseEvent):void
		{
			_mapLayer.stopDrag();
			_stage.removeEventListener(Event.ENTER_FRAME,frameHandler);
			_stage.removeEventListener(MouseEvent.MOUSE_UP,touchEndHandler);
			isOverScreen();
			if(_paopaoComp)_paopaoComp.visible = false;
		}
		
		protected function frameHandler(event:Event):void
		{
			isOverScreen();
		}
		
		private function isOverScreen():void
		{
			if(_mapLayer.x > 0)
			{
				_mapLayer.x = 0;
			}else if(_mapLayer.x < _stage.stageWidth - _mapLayer.width)
			{
				_mapLayer.x = _stage.stageWidth - _mapLayer.width;
			}
			if(_mapLayer.y > 0)
			{
				_mapLayer.y = 0;
			}else if(_mapLayer.y < _stage.stageHeight - _mapLayer.height)
			{
				_mapLayer.y = _stage.stageHeight - _mapLayer.height;
			}
		}
		
		private function center(offX:Number,offY:Number):void
		{
			offX = offX * _mapLayer.scaleX;
			offY = offY * _mapLayer.scaleY;
			var offx:Number = - (offX + _mapLayer.x) + _stage.stageWidth / 2;
			var offy:Number = - (offY + _mapLayer.y) + _stage.stageHeight / 2;
			if(_mapLayer.x + offx > 0)
			{
				offx = - _mapLayer.x;
			}else if(offx + _mapLayer.x < _stage.stageWidth - _mapLayer.width)
			{
				offx = _stage.stageWidth - _mapLayer.width - _mapLayer.x;
			}
			if(_mapLayer.y + offy > 0)
			{
				offy = - _mapLayer.y;
			}else if(offy + _mapLayer.y< _stage.stageHeight - _mapLayer.height)
			{
				offy = _stage.stageHeight - _mapLayer.height - _mapLayer.y;
			}
			TweenLite.to(_mapLayer,0.8,{x:_mapLayer.x + offx,y:_mapLayer.y + offy})
		}
		
		private function changeMapScale(type:int):void
		{
			var compareMap:Number;
			var compareStage:Number;
			if(_map.height < _map.width)
			{
				compareMap = _map.height;
				compareStage = _stage.stageHeight;
			}else
			{
				compareMap = _map.width;
				compareStage = _stage.stageWidth;
			}
			
			if(type == 1)//放大
			{
				if(compareMap <= compareStage * 1.4)
				{
					changeBackSize(type);
				}else
				{
					trace("已经是最大尺寸了");
				}
			}else
			{
				if(compareMap >= compareStage * 1.2)
				{
					changeBackSize(type);
				}else
				{
					trace("已经是最小尺寸了");
				}
			}
		}
		
		private function changeBackSize(type:int):void
		{
			var oldWid:Number = _map.width;
			var oldHei:Number = _map.height;
			if(type == 1)//放大
			{
				if(_map.height < _map.width)
				{
					_map.height = _map.height + _stage.stageHeight * 0.2;
					_map.width = _map.height * oldWid / oldHei;
				}else
				{
					_map.width = _map.width + _stage.stageWidth * 0.2;
					_map.height = _map.width * oldHei / oldWid;
				}
				_mapLayer.y = _mapLayer.y + (oldHei - _map.height) * 0.5;
				_mapLayer.x = _mapLayer.x + (oldWid - _map.width) * 0.5;
			}else
			{
				if(_map.height < _map.width)
				{
				_map.height = _map.height - _stage.stageHeight * 0.2;
				_map.width = _map.height * oldWid / oldHei;
				}else
				{
					_map.width = _map.width - _stage.stageWidth * 0.2;
					_map.height = _map.width * oldHei / oldWid;
				}
			_mapLayer.y = _mapLayer.y + (oldHei - _map.height) * 0.5;
			_mapLayer.x = _mapLayer.x + (oldWid - _map.width) * 0.5;
			if(_mapLayer.x > 0)_mapLayer.x = 0;
			if(_mapLayer.y > 0)_mapLayer.y = 0;
			}
			autosizeArrow(_map.width/oldWid,_map.height/oldHei);
		}
		
		private function changeArrowsVisible():void
		{
			var arrow:ArrowItem;
			for(var i:int=0;i<_arrowList.length;i++)
			{
				arrow = _arrowList[i];
				arrow.visible = !arrow.visible;
			}
		}
		
		private function showWc():void
		{
			if(_wcList.length == 0)
			{
			for(var i:int=0;i<_data.wcs.length;i++)
			{
				var arrow:WCItem = new WCItem(_loader);
				arrow.x = _data.wcs[i].posX;
				arrow.y = _data.wcs[i].posY;
				_mapLayer.addChild(arrow);
				_wcList.push(arrow);
			}
			}else
			{
				var wcItem:WCItem;
				for(var j:int=0;j<_wcList.length;j++)
				{
					wcItem = _wcList[j];
					wcItem.visible = !wcItem.visible;
					wcItem.update();
				}
			}
		}
		
		private function autosizeArrow(scaleWid:Number,scaleHei:Number):void
		{
			var arrow:ArrowItem;
			for(var i:int=0;i<_arrowList.length;i++)
			{
				arrow = _arrowList[i];
				arrow.x = arrow.x * scaleWid;
				arrow.y = arrow.y * scaleHei;
			}
		}
		
	}
}